﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VCLib.Network;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        VCLib.Network.UDPServer UDPServer;
        private readonly string url;
        public MainWindow()
        {
            InitializeComponent();
            UDPServer = new VCLib.Network.UDPServer(VCLib.Util.ConfigHelp.ReadConfig<int>("port"));
            url = VCLib.Util.ConfigHelp.ReadConfig("url");
            UDPServer.DataReceivedEvent += UDPServer_DataReceivedEvent;
            UDPServer.SocketServerStateEvent += UDPServer_SocketServerStateEvent;
            UDPServer.Open();
            this.Closed += MainWindow_Closed;
        }

        private void MainWindow_Closed(object sender, EventArgs e)
        {
            UDPServer.Dispose();
        }

        private void UDPServer_SocketServerStateEvent(object sender, SocketServerStateEventArg arg)
        {
            log(arg.Message);
        }

        private byte[] UDPServer_DataReceivedEvent(object sender, VCLib.Network.Entity.SocketServerReceivedDataArg args)
        {
            try
            {
                var str = Encoding.UTF8.GetString(args.Data);
                string sendUrl=string.Format(url, str);
                Task.Run(() => { httpGet(sendUrl); });
                
            }
            catch (Exception ex)
            {
                log(ex.Message);
                
            }
            return null;
        }
        public void httpGet(string url)
        {
            try
            {
                log($"发送 http get:{url}");
                var res = VCLib.Network.HTTPServers.HttpSendHelp.CreateGetHttpResponse(url, 2000, null, null);
                log($"发送 http get成功 ");
            }
            catch (Exception ex)
            {
                log($"发送 http get失败 :{ex.Message}");
            }
           
        }
        int num;
        void log(string message)
        {
            if (Thread.CurrentThread != Dispatcher.Thread)
            {
                Dispatcher.BeginInvoke(new Action(() => { log(message); }));
                return;
            }
            num++;
            if (num > 300)
            {
                richTextBox.Document.Blocks.Clear(); num = 0;
            }
            richTextBox.AppendText(DateTime.Now.ToString()+"  " +message + "\n");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var arg = u.Text;

            string sendUrl = string.Format(url, arg);
            Task.Run(() => { httpGet(sendUrl); });
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var sendUrl = u.Text;
            Task.Run(() => { httpGet(sendUrl); });
        }
    }
}
